<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-03-22
 * Time: 16:37
 */
namespace app\admin\controller;
use Sunra\PhpSimple\HtmlDomParser;
use think\Db;
use think\Exception;

class Operate extends Base
{
    /**
     * 空操作
     */
    public function _empty(){
        if($this->check_admin_login()){
            $this->redirect('admin/Index/index');
        }else{
            $this->redirect('admin/login');
        }
    }

    /**
     * 轮播列表
     */
    public function carousel_list(){
        $page_param = input('param.');
        if (isset($page_param['page']) && $page_param['page'] > 0)
            unset($page_param['page']);

        $carousel_list = Db::table('yf_carousel')->order('sort asc')->paginate(20, false, ['query' => $page_param]);
        //分页信息
        $page = $carousel_list->render();

        $carousel_list = $carousel_list->toArray();
        if ($carousel_list['data']){
            foreach ($carousel_list['data'] as $k=>$v){
                if ($v['cate'] ===1){
                    $carousel_list['data'][$k]['shop_name'] = Db::name('shop')->where(['id'=>$v['item_content']])->value('shop_name');
                }
                $carousel_list['data'][$k]['image'] = get_img_url($v['image']);
            }

        }
        //模板变量赋值
        $this->assign('list', $carousel_list['data']);
        $this->assign('total', $carousel_list['total']);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }

    //轮播排序
    public function word_order(){
        if (!$this->request->isPost()){
            return json(['code'=>0,'msg'=>'提交方式不正确']);
        }else{
            foreach ($_POST as $id => $sort){
                Db::name('carousel')->where(array('carousel_id' => $id ))->setField('sort' , $sort);
            }
            return json(['code'=>200,'msg'=>'排序更新成功']);
        }
    }

    /**
     * 轮播添加
     */
    public function carousel_add(){
        if ($this->request->isPost()){
            //开启事务
            Db::startTrans();
            try{
                $data['title'] = input('param.title',''); //轮播标题
                $data['position'] = input('param.position',0); //轮播标题
                $data['cate'] = input('param.cate',0); //类型
                $data['image'] = input('param.image','');//图片地址
                $data['start_time'] = input('param.start_time',0);//开始时间
                $data['end_time'] = input('param.end_time',0);//结束时间
                $data['status'] = input('param.status',0);//状态
                if ($data['position'] == 0){
                    throw new Exception('请选择投放的位置');
                }
                if ($data['cate'] == 0){
                    throw new Exception('请选择轮播类型');
                }elseif ($data['cate'] == 2){
                    $data['url'] = input('param.url',''); //链接地址
                    if ($data['url'] == ''){
                        throw new Exception('链接地址不能为空');
                    }
                }elseif ($data['cate'] == 4){
                    $data['itemid'] = input('param.itemid',0);//商品id
                    $data['shoptype'] = input('param.shoptype','C');//商品类型
                    if ($data['itemid'] == 0){
                        throw new Exception('商品id不能为空');
                    }
                }
                if ($data['image'] == ''){
                    throw new Exception('封面图未上传');
                }
                if ($data['start_time'] == 0){
                    throw new Exception('开始时间未设置');
                }
                if ($data['end_time'] == 0){
                    throw new Exception('结束展示时间未设置');
                }
                $data['start_time'] = date_to_unixtime( $data['start_time']);//开始展示时间 某年某日00时00分00秒
                $data['end_time'] = date_to_unixtime( $data['end_time'])+86399;//结束展示时间 某年某日23时59分59秒
                $data['add_time'] = time();//创建时间
                $data['edit_time'] = time();//编辑时间
                $data['sort'] = 50;//排序
                $data['add_admin'] = session('admin_auth.aid');//发布者
                $id = Db::table('yf_carousel')->insertGetId($data);
                if ( $data['cate'] == 3){
                    Db::table('yf_carousel')->where('carousel_id',$id)->setField('subject_id',$id);
                }
                //事务提交
                Db::commit();
                return json(['code'=>200,'msg'=>'success']);
            }catch (Exception $e){
                //事务回滚
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            // 渲染模板输出
            return $this->fetch();
        }
    }
    /**
     * 轮播编辑
     */
    public function carousel_edit(){
        if ($this->request->isPost()){
            //开启事务
            Db::startTrans();
            try{
                $data['carousel_id'] = input('param.carousel_id',''); //轮播id
                $data['title'] = input('param.title',''); //轮播标题
                $data['cate'] = input('param.cate',0); //类型
                $data['item_content'] = input('param.item_content',''); //类型内容
                $data['image'] = input('param.image','');//图片地址
                $data['start_time'] = input('param.start_time',0);//开始时间
                $data['end_time'] = input('param.end_time',0);//结束时间
                $data['status'] = input('param.status',0);//状态
                if ($data['cate'] == 0){
                    throw new Exception('类型未选择');
                }
                if ($data['item_content'] == ''){
                    throw new Exception('类型未选择');
                }
                if ($data['image'] == ''){
                    throw new Exception('类型未选择');
                }
                if ($data['start_time'] == 0){
                    throw new Exception('开始时间不能为空');
                }
                if ($data['end_time'] == 0){
                    throw new Exception('结束时间不能为空');
                }
                $data['start_time'] = date_to_unixtime( $data['start_time']);
                $data['end_time'] = date_to_unixtime( $data['end_time']);
                Db::table('yf_carousel')->update($data);
                //事务提交
                Db::commit();
                return json(['code'=>200,'msg'=>'success']);
            }catch (Exception $e){
                //事务回滚
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            $carousel_info=Db::name('carousel')->where(array('carousel_id'=>input('carousel_id')))->find();
            if ($carousel_info){
                $carousel_info['host_image'] = get_img_url($carousel_info['image']);
                $carousel_info['str'] = '';
                if ($carousel_info['cate'] == 1){
                    $shop_list = Db::name('shop')->field('id,shop_name')->select();
                    foreach ($shop_list as $k=>$v){
                        if ($carousel_info['item_content'] == $v['id']){
                            $carousel_info['str'] = $carousel_info['str'].'<option value="'.$v['id'].'" selected>'.$v['shop_name'].'</option>';
                        }else{
                            $carousel_info['str'] = $carousel_info['str'].'<option value="'.$v['id'].'">'.$v['shop_name'].'</option>';
                        }

                    }
                    $carousel_info['str'] = ' <span class="select-box" style="width: auto"> <select name="item_content" class="select" style="width: auto">'.$carousel_info['str']. '</select></span>';

                }elseif ($carousel_info['cate'] == 2){
                    $carousel_info['str'] = '<input type="text" class="input-text"  name="item_content" value="'.$carousel_info['item_content'].'" placeholder="请输入要链接的网址">';
                }
            }
            $this->assign('data',$carousel_info);
            // 渲染模板输出
            return $this->fetch();
        }
    }

    //轮播删除
    public function carousel_del(){
        $carousel_id=input('carousel_id','','intval');
        Db::startTrans();
        try{
            if (!is_numeric($carousel_id)){
                throw new Exception('轮播id错误');
            }
            $rst=Db::name('carousel')->where('carousel_id',$carousel_id)->delete();
            if($rst!==false){
                Db::commit();
                return json(['code'=>200,'msg'=>'轮播删除成功']);
            }else{
                throw new Exception('轮播删除失败');
            }
        }catch (Exception $e){
            Db::rollback();
            return json(['code'=>0,'msg'=>$e->getMessage()]);
        }
    }


    /**
     *商品列表
     */
    public function item_list(){
        $page_param = input('param.');
        if (isset($page_param['page']) && $page_param['page'] > 0)
            unset($page_param['page']);
        $item_cates = db('items_cate')->field('id,name,parent_id')->where('status',1)->select(); //类目
        $this->assign('item_cates',$item_cates);
        $gets['goods_cate'] = request()->get('goods_cate',0);//商品类型
        $gets['min_price'] = request()->get('min_price','');//最低价
        $gets['max_price'] = request()->get('max_price','');//最高价
        $gets['sales_volume'] = request()->get('sales_volume','');//最低销量
        $gets['key_word'] = request()->get('key_word','');//关键词
        $gets['sort'] = request()->get('sort',0);//排序
        if ($gets['sort'] == 0){
            $sort = 'product_id desc';
        }elseif ($gets['sort'] == 1){
            $sort = 'itemendprice asc';
        }elseif ($gets['sort'] == 2){
            $sort = 'itemendprice desc';
        }elseif ($gets['sort'] == 3){
            $sort = 'couponmoney desc';
        }elseif ($gets['sort'] == 4){
            $sort = 'itemsale desc';
        }else{
            $sort = 'tkrates desc';
        }
        $min_price= $gets['min_price'] == '' ? 0:$gets['min_price'];
        $max_price= $gets['max_price'] == '' ? 0:$gets['max_price'];
        $sales_volume= $gets['sales_volume'] == '' ? 0:$gets['sales_volume'];
        if ( $min_price != 0 && $max_price != 0){
            $where['a.itemendprice'] = ['between',$min_price.','.$max_price];
        }elseif ($min_price == 0 && $max_price != 0){
            $where['a.itemendprice'] = ['lt',$max_price];
        }else if ($min_price != 0 && $max_price == 0){
            $where['a.itemendprice'] = ['gt',$min_price];
        }
        if ($gets['key_word'] != ''){
            $where['a.itemtitle'] = ['like','%'.$gets['key_word'].'%'];
        }
        $where['itemsale'] = ['gt',$sales_volume];
        if ($gets['goods_cate'] != 0){
            $where['a.fqcat'] = $gets['goods_cate'];
        }
        $itemlist = Db::name('items')->field('a.*,b.name cate_name')->alias('a')->join('yf_items_cate b','a.fqcat=b.id')->where($where)->order($sort)->paginate(20, false, ['query' => $page_param]);
        //分页信息
        $page = $itemlist->render();
        $itemlist = $itemlist->toArray();
        if (!empty($itemlist)){
            foreach ($itemlist['data'] as $k=>$v){
                $tuijian = Db::name('recommend')->where('itemid',$v['itemid'])->value('tuijian');
                if ($tuijian){
                    $itemlist['data'][$k]['tuijian'] = $tuijian;
                }else{
                    $itemlist['data'][$k]['tuijian'] = 0;
                }
            }
        }
        //模板变量赋值
        $this->assign('gets',$gets);
        $this->assign('list', $itemlist['data']);
        $this->assign('total', $itemlist['total']);
        $this->assign('page', $page);

        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 设置首页推荐类型
     */
    public function set_tuijian(){
        $itemid = input('param.id','');
        $tuijian = input('param.tuijian',0);
        if (empty($itemid)){
            return json(['code'=>0,'msg'=>'商品id未传']);
        }
        //开启事务
        Db::startTrans();
        try{
            if ($tuijian == '0'){
                Db::name('recommend')->where('itemid',$itemid)->delete();
            }else{
                $num = Db::name('recommend')->where('itemid',$itemid)->count();
                if ($num >0){
                    Db::name('recommend')->where('itemid',$itemid)->setField('tuijian',$tuijian);
                }else{
                    $item_info = Db::name('items')->field('itemid,itemtitle,itemshorttitle,itemprice,itemsale,itempic,itemendprice,couponurl,shoptype,shoptype,couponmoney,couponstarttime,couponendtime,activityid')->where('itemid',$itemid)->find();
                    if (!empty($item_info)){
                        $item_info['tuijian'] = $tuijian;
                        $item_info['add_time'] = time();
                        $flag = Db::name('recommend')->insert($item_info);
                        if ($flag == false){
                            throw new Exception('设置失败');
                        }
                    }else{
                        throw new Exception('无该商品的数据信息');
                    }
                }
            }
            Db::commit();
            return json(['code'=>200,'msg'=>'success']);
        }catch (Exception $e){
            Db::rollback();
            return json(['code'=>0,'msg'=>$e->getMessage()]);
        }
    }

    /**
     * 专题管理
     */
    public function subject_list(){
        $page_param = input('param.');
        if (isset($page_param['page']) && $page_param['page'] > 0)
            unset($page_param['page']);

        $itemlist = Db::name('subject')->paginate(20, false, ['query' => $page_param]);
        //分页信息
        $page = $itemlist->render();
        $itemlist = $itemlist->toArray();
        if (!empty($itemlist['data'])){
            foreach ($itemlist['data'] as $k=>$v){
                $itemlist['data'][$k]['cover_img'] = get_img_url($v['cover_img']);
            }
        }
        //模板变量赋值
//        $this->assign('gets',$gets);
        $this->assign('list', $itemlist['data']);
        $this->assign('total', $itemlist['total']);
        $this->assign('page', $page);

        // 渲染模板输出
        return $this->fetch();
    }

    /**
     * 专题添加
     */
    public function subject_add(){
        if ($this->request->isPost()){
            //开启事务
            Db::startTrans();
            try{
                $data['name'] = input('param.name',''); //专题标题
                $data['bannerid'] = input('param.bannerid',0); //属于哪个banner展示位
                $data['desc'] = input('param.desc',''); //专题描述
                $data['cover_img'] = input('param.cover_img','');//图片地址
                $data['begin_time'] = input('param.begin_time',0);//开始时间
                $data['end_time'] = input('param.end_time',0);//结束时间
                $data['status'] = input('param.status',0);//状态
                $data['sort'] = input('param.sort',50);//排序
                if ($data['name'] == ''){
                    throw new Exception('专题标题不能为空');
                }
                if ($data['bannerid'] == 0){
                    throw new Exception('请选择要上的轮播位置');
                }
                if ($data['cover_img'] == ''){
                    throw new Exception('封面图未传');
                }
                if ($data['begin_time'] == 0){
                    throw new Exception('开始时间不能为空');
                }
                if ($data['end_time'] == 0){
                    throw new Exception('结束时间不能为空');
                }
                $data['begin_time'] = date_to_unixtime( $data['begin_time']);
                $data['end_time'] = date_to_unixtime( $data['end_time']);
                $data['add_time'] = time();//结束时间
                Db::table('yf_subject')->insert($data);
                //事务提交
                Db::commit();
                return json(['code'=>200,'msg'=>'success']);
            }catch (Exception $e){
                //事务回滚
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 专题编辑
     */
    public function subject_edit(){
        if ($this->request->isPost()){
            //开启事务
            Db::startTrans();
            try{
                $data['subject_id'] = input('param.subject_id',''); //专题标题
                $data['name'] = input('param.name',''); //专题标题
                $data['bannerid'] = input('param.bannerid',0); //属于哪个banner展示位
                $data['desc'] = input('param.desc',''); //专题描述
                $data['cover_img'] = input('param.cover_img','');//图片地址
                $data['begin_time'] = input('param.begin_time',0);//开始时间
                $data['end_time'] = input('param.end_time',0);//结束时间
                $data['status'] = input('param.status',0);//状态
                $data['sort'] = input('param.sort',50);//排序
                if ($data['subject_id'] == '' || $data['subject_id'] == 0 ){
                    throw new Exception('专题id不能为空');
                }
                if ($data['name'] == ''){
                    throw new Exception('专题标题不能为空');
                }
                if ($data['bannerid'] == 0){
                    throw new Exception('请选择要上的轮播位置');
                }
                if ($data['cover_img'] == ''){
                    throw new Exception('封面图未传');
                }
                if ($data['begin_time'] == 0){
                    throw new Exception('开始时间不能为空');
                }
                if ($data['end_time'] == 0){
                    throw new Exception('结束时间不能为空');
                }
                $data['begin_time'] = date_to_unixtime( $data['begin_time']);
                $data['end_time'] = date_to_unixtime( $data['end_time']);
                Db::table('yf_subject')->update($data);
                //事务提交
                Db::commit();
                return json(['code'=>200,'msg'=>'success']);
            }catch (Exception $e){
                //事务回滚
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            $subject_info=Db::name('subject')->where(array('subject_id'=>input('subject_id')))->find();
            if ($subject_info){
                $subject_info['host_image'] = get_img_url($subject_info['cover_img']);
            }
            $this->assign('data',$subject_info);
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 专题商品删除
     */
    public function subject_del()
    {
        $subject_id = input('subject_id','','intval');//专题id
        if (!is_numeric($subject_id)){
            return json(['code'=>0,'msg'=>'专题id错误']);
        }
        //删除专题下面的商品
        $rst=Db::name('subject_items')->where('subject_id',$subject_id)->delete();
        if($rst!==false){
            //删除专题
            $flag = Db::name('subject')->where('subject_id',$subject_id)->delete();
            if ($flag!==false){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>0,'msg'=>'专题删除失败']);
            }
        }else{
            return json(['code'=>0,'msg'=>'专题下的商品删除失败']);
        }
    }


    /**
     * 专题关联商品列表
     */
    public function subject_items(){
        $page_param = input('param.');
        if (isset($page_param['page']) && $page_param['page'] > 0)
            unset($page_param['page']);
        $subject_id = input('param.subject_id',0); //专题id
        $subject_items_list = Db::table('yf_subject_items')->where(['subject_id'=>$subject_id])->order('sort desc,id desc')->paginate(20, false, ['query' => $page_param]);
        //分页信息
        $page = $subject_items_list->render();
        $subject_items_list = $subject_items_list->toArray();
        //模板变量赋值
        $this->assign('subject_id',$subject_id);
        $this->assign('list', $subject_items_list['data']);
        $this->assign('total', $subject_items_list['total']);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch();
    }


    /**
     * 关联商品添加
     */
    public function subject_items_add(){
        if ($this->request->isPost()){
            //开启事务
            Db::startTrans();
            try{
                $data['itemtitle'] = input('param.itemtitle',''); //标题
                $data['itemprice'] = input('param.itemprice',0); //价格
                $data['subject_id'] = input('param.subject_id',0); //淘宝id
                $data['itemid'] = input('param.itemid',0); //好物淘宝id
                if($data['itemid'] !=0){
                    $num = Db::table('yf_subject_items')->where(['itemid'=>$data['itemid'],'subject_id'=>$data['subject_id']])->count();
                    if ($num >0){
                        throw new Exception('商品已存在，请误重复添加该商品');
                    }
                }
                $data['itempic'] = input('param.itempic',''); //好物图片链接
                $data['itemdesc'] = input('param.itemdesc','');//好物推荐理由
                $data['shoptype'] = input('param.shoptype','C');//好物所属商店类型
                $data['itemsale'] = input('param.itemsale',0);//好物月销量
                $data['coupon'] = input('param.coupon',0);//是否有优惠券 0无 1有
                if ($data['coupon'] == 1){
                    $data['couponurl'] = input('param.couponurl','');//优惠券链接
                    $data['couponmoney'] = input('param.couponmoney',0);//优惠券金额
                    $data['couponstarttime'] = input('param.couponstarttime',0);//优惠券开始时间
                    $data['couponendtime'] = input('param.couponendtime',0);//优惠券结束时间
                    $data['itemendprice'] =  $data['itemprice'] - $data['couponmoney'];//券后价
                }else{
                    $data['itemendprice'] =  $data['itemprice'];
                }
                if ($data['shoptype'] == ''){
                    throw new Exception('请选择天猫或者淘宝');
                }
                if ($data['coupon'] == 1 && (empty($data['couponurl']) || empty($data['couponmoney']))){
                    throw new Exception('优惠券信息有误');
                }
                if ($data['coupon'] == 1 ){
                    if($data['couponmoney'] > $data['itemprice'])
                    throw new Exception('优惠券金额或者在售价信息有误');
                }
                $data['add_time'] = time();//结束时间
                $data['sort'] = 50;//排序
                Db::table('yf_subject_items')->insert($data);
                //事务提交
                Db::commit();
                return json(['code'=>200,'msg'=>'success']);
            }catch (Exception $e){
                //事务回滚
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            $subject_id = input('param.subject_id',0);
            $this->assign('subject_id',$subject_id);
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 专题商品编辑
     */
    public function subject_items_edit(){
        if ($this->request->isPost()){
            //开启事务
            Db::startTrans();
            try{
                $data['id'] = input('param.id',0); //关联商品id
                if ($data['id'] == 0){
                    throw new Exception('关联商品id未传');
                }
                $data['itemtitle'] = input('param.itemtitle',''); //商品标题
                $data['itemprice'] = input('param.itemprice',0); //商品在售价格
                $data['itemid'] = input('param.itemid',0); //商品淘宝id
                $data['itempic'] = input('param.itempic',''); //商品图片链接
                $data['itemdesc'] = input('param.itemdesc','');//商品推荐理由
                $data['shoptype'] = input('param.shoptype','C');//商品所属商店类型
                $data['itemsale'] = input('param.itemsale',0);//好物月销量
                $data['sort'] = input('param.sort',50);//好物月销量
                $data['coupon'] = input('param.coupon',0);//是否有优惠券 0无 1有
                if ($data['coupon'] == 1){
                    $data['couponurl'] = input('param.couponurl','');//优惠券链接
                    $data['couponmoney'] = input('param.couponmoney',0);//优惠券金额
                    $data['itemendprice'] =  $data['itemprice'] - $data['couponmoney'];//券后价
                }else{
                    $data['itemendprice'] =  $data['itemprice'];
                }
                if ($data['shoptype'] == ''){
                    throw new Exception('请选择天猫或者淘宝');
                }
                if ($data['coupon'] == 1 && (empty($data['couponurl']) || empty($data['couponmoney']))){
                    throw new Exception('优惠券信息有误');
                }
                Db::table('yf_subject_items')->update($data);
                //事务提交
                Db::commit();
                return json(['code'=>200,'msg'=>'success']);
            }catch (Exception $e){
                //事务回滚
                Db::rollback();
                return json(['code'=>0,'msg'=>$e->getMessage()]);
            }
        }else{
            $good_info=Db::name('subject_items')->where(array('id'=>input('id')))->find();
            if ($good_info){
                $good_info['itempic'] = get_img_url($good_info['itempic']);
            }
            $this->assign('data',$good_info);
            // 渲染模板输出
            return $this->fetch();
        }
    }

    /**
     * 专题商品删除
     */
    public function subject_items_del()
    {
        $id=input('id','','intval');
        if (!is_numeric($id)){
            return json(['code'=>0,'msg'=>'专题商品id错误']);
        }
        $rst=Db::name('subject_items')->where('id',$id)->delete();
        if($rst!==false){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        }
    }


    /**
     * 根据淘宝、天猫url一键获取商品信息
     */
    public function ajax_getiteminfo(){
        $item_url = input('param.item_url','');
        try{
            if (empty($item_url)){
                throw new Exception('商品链接不能为空!');
            }
            $data = [];
            $arr = parse_url($item_url);
            parse_str(@array_pop(explode('?', $item_url)), $a);
            $data['itemid'] = $a['id'];
            if ($arr['host'] == 'detail.tmall.com'){
                $data['shoptype'] = 'B';
            }else if ($arr['host'] == 'item.taobao.com'){
                $data['shoptype'] = 'C';
            }else{
                throw new Exception('所选商品非淘宝/天猫商品!');
            }
            $goods_info = Db::name('items')->field('itemtitle,itemdesc,itemprice,itemsale,itempic,couponurl,couponmoney,start_time,end_time')->where(['itemid'=>$data['itemid']])->find();
            $dom = HtmlDomParser::file_get_html($item_url);
            if ($goods_info){
                $data['title'] = $goods_info['itemtitle'];
                $data['price'] = $goods_info['itemprice'];
                $data['desc'] = $goods_info['itemdesc'];
                $data['sell_counter'] = $goods_info['itemsale'];
            }else{
                $data['title'] = mb_convert_encoding($dom->find('title',0)->innertext(), "UTF-8", "GBK");
                $data['price'] = '';
                $data['desc'] = '';
                $data['sell_counter'] = '';
            }
            if ($data['shoptype'] == 'B'){
                $data['itempic'] = 'http:'.mb_convert_encoding($dom->find('#J_ImgBooth',0)->src, "UTF-8", "GBK");//商品图片
            }else{
                $data['itempic'] = 'http:'.mb_convert_encoding($dom->find('#J_ImgBooth',0)->src, "UTF-8", "GBK");//商品图片
            }
            return json(['code'=>200,'msg'=>'success','data'=>$data]);
        }catch (Exception $e){
            return json(['code'=>0,'msg'=>$e->getMessage()]);
        }

    }

    //专题关联商品排序
    public function subject_items_order(){
        if (!$this->request->isPost()){
            return json(['code'=>0,'msg'=>'提交方式不正确']);
        }else{
            foreach ($_POST as $id => $sort){
                Db::name('subject_items')->where(array('id' => $id ))->setField('sort' , $sort);
            }
            return json(['code'=>200,'msg'=>'排序更新成功']);
        }
    }





}